package ltd.newbee.mall.service.impl;


import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.dao.ISupplierInfoDAO;
import ltd.newbee.mall.entity.NewbeeMallSupplierInfo;
import ltd.newbee.mall.enums.ResultEnum;
import ltd.newbee.mall.exception.MarketException;
import ltd.newbee.mall.service.ISupplierInfoService;
import ltd.newbee.mall.util.PageResult;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Optional;

/**
 * @ClassName: SupplierInfoServiceImpl
 * @Description: 供货商信息Service接口的实现类
 * @Author: Emily
 * @Date: 2021-09-03 10:51
 * @Version 1.0
 */
@Service
public class SupplierInfoServiceImpl implements ISupplierInfoService
{
    @Autowired
    private ISupplierInfoDAO supplierInfoDAO;

    @Override
    public Optional<NewbeeMallSupplierInfo> findById(String supplierId)
    {
        return supplierInfoDAO.findById(supplierId);
    }

    @Override
    public List<NewbeeMallSupplierInfo> findByName(String supplierName)
    {
        return supplierInfoDAO.queryBySupplierName(supplierName);
    }

    @Override
    public List<NewbeeMallSupplierInfo> findAll() {
        return supplierInfoDAO.findAll();
    }

    @Override
    public Page<NewbeeMallSupplierInfo> findAll(Pageable pageable) {
        return supplierInfoDAO.findAll(pageable);
    }

    @Override
    public PageResult getNewBeeMallSupplierPage(PageRequest pageRequest) {
        Page<NewbeeMallSupplierInfo> supplierInfoPageList = supplierInfoDAO.findAll(pageRequest);
        List<NewbeeMallSupplierInfo> newbeeMallSupplierInfos = supplierInfoPageList.getContent();

        int total = (int)supplierInfoDAO.count();

        PageResult pageResult = new PageResult(newbeeMallSupplierInfos, total, pageRequest.getPageSize(), pageRequest.getPageNumber() + 1);

        return  pageResult;
    }

    @Override
    public String save(NewbeeMallSupplierInfo newbeeMallSupplierInfo) {

//        NewbeeMallSupplierInfo newbeeMallSupplierInfo_new = new NewbeeMallSupplierInfo();
//        if (StringUtils.hasText(newbeeMallSupplierInfo.getSupplierId()))
//        {
//            Optional<NewbeeMallSupplierInfo> newbeeMallSupplierInfoOptional = supplierInfoDAO.findById(newbeeMallSupplierInfo.getSupplierId());
//            newbeeMallSupplierInfo_new = newbeeMallSupplierInfoOptional.get();
//        }

        try
        {
            supplierInfoDAO.save(newbeeMallSupplierInfo);
            return ServiceResultEnum.SUCCESS.getResult();
        }catch (Exception e)
        {
            return ServiceResultEnum.DB_ERROR.getResult();
        }
    }

    @Override
    public String modify(NewbeeMallSupplierInfo newbeeMallSupplierInfo)
    {
        if (findById(newbeeMallSupplierInfo.getSupplierId()).isPresent()) {
            supplierInfoDAO.save(newbeeMallSupplierInfo);
            return ServiceResultEnum.SUCCESS.getResult();
        } else {
            //抛出自定义异常供货商不存在
            return ServiceResultEnum.SUPPLIER_NOT_EXIST_ERROR.getResult();
        }
    }

    @Override
    public void deleteById(String supplierId)
    {
        supplierInfoDAO.deleteById(supplierId);
    }
}
